//
//  CarCenterViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/10/27.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "CarCenterViewController.h"

#import "ShopView.h"
#import "ShopCenterCell.h"

#import "MessViewController.h"
#import "DJDViewController.h"
#import "YJDViewController.h"
#import "YWCViewController.h"
#import "PingJiaViewController.h"
#import "YueViewController.h"
#import "KFuViewController.h"

#import "HouseMaGerViewController.h"
#import "DPZLViewController.h"
@interface CarCenterViewController ()<UITableViewDelegate,UITableViewDataSource>{
    ShopView *shop;
    NSArray *urlArr;
    
    NSString *_mer_id;
}

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableDictionary *dic;

@property (nonatomic,strong)NSArray *arr1Img;
@property (nonatomic,strong)NSArray *arr1Tit;
@property (nonatomic,strong)NSMutableArray *arr1Det;
@property (nonatomic,strong)NSArray *arr2Img;
@property (nonatomic,strong)NSArray *arr2Tit;
@property (nonatomic,strong)NSArray *arr3Img;
@property (nonatomic,strong)NSArray *arr3Tit;

@property (nonatomic, strong) NSMutableArray *commentUrlArray;//评论接口

@end

@implementation CarCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    _dic = [NSMutableDictionary dictionary];
    
    _arr1Img = @[@"wdye",@"评论-1"];
    _arr1Tit = @[@"我的余额",@"店铺评论"];
    _arr1Det = [NSMutableArray arrayWithArray:@[@"200元",@"2222"]];
    
    _arr2Img = @[@"商品管理-拷贝",@"053-店铺资料-拷贝"];
    _arr2Tit = @[@"商品管理",@"店铺资料"];
    
    _arr3Img = @[@"用户",@"问题-骑手问题"];
    _arr3Tit = @[@"推广用户",@"推广骑手"];
    
    //路径
    urlArr = @[@"",@"/App/Merchantcentre/housing",@"/App/Merchantcentre/carbuying",@"/App/Merchantcentre/cleaning",@"/App/Merchantcentre/unlocking",@"/App/Merchantcentre/vegetables",@"/App/Merchantcentre/coolie",@"",@"/App/Merchantcentre/housekeeping",@"/App/Merchantcentre/carwash",@""];
    
    self.commentUrlArray = [[NSMutableArray alloc]initWithObjects:@"",@"/App/Merchantcomment/buyacar",@"/App/Merchantcomment/carwash",@"/App/Merchantcomment/purchase",@"/App/Merchantcomment/onthdoor",@"/App/Merchantcomment/coolie",@"/App/Merchantcomment/cleaning",@"/App/Merchantcomment/housekeeping",@"/App/Merchantcomment/unlocking",@"/App/Merchantcomment/vegetables", nil];
    
    [self loadNav];
    [self loadData];
}

- (void)loadNav{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    view.backgroundColor = [UIColor whiteColor];
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"商家中心";
    lab.textColor = [UIColor blackColor];
    lab.font = [UIFont systemFontOfSize:18.0];
    [view addSubview:lab];
    [lab makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(34);
    }];
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:line];
    [line makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.bottom.equalTo(view.bottom);
        make.height.equalTo(0.5);
    }];
    
    //返回按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    [btn makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.left.equalTo(5);
        make.width.equalTo(30);
        make.height.equalTo(30);
    }];
    
    //按钮
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setImage:[UIImage imageNamed:@"消息2"] forState:UIControlStateNormal];
    [btn2 setImage:[UIImage imageNamed:@"消息2"] forState:UIControlStateHighlighted];
    [btn2 addTarget:self action:@selector(clickRight) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn2];
    [btn2 makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.right.equalTo(-5);
        make.width.equalTo(30);
        make.height.equalTo(30);
    }];
    
}
// 返回
- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)clickRight{
    MessViewController *mess = [[MessViewController alloc] init];
    [self.navigationController pushViewController:mess animated:YES];
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    shop = [[[NSBundle mainBundle] loadNibNamed:@"ShopView" owner:nil options:nil] lastObject];
    shop.frame = CGRectMake(0, 0, ScreenWidth, 220);
    _tableView.tableHeaderView = shop;
    
    shop.view1.layer.cornerRadius = 5.0;
    shop.view1.layer.borderWidth = 0.5;
    shop.view1.layer.borderColor = [UIColor blackColor].CGColor;
    
    shop.view1.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickWork)];
    [shop.view1 addGestureRecognizer:tap];
    
    shop.view2.layer.cornerRadius = 5.0;
    shop.view2.layer.borderWidth = 0.5;
    shop.view2.layer.borderColor = [UIColor blackColor].CGColor;
    
    shop.labKQ.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickKQ)];
    NSString *automid = [NSString stringWithFormat:@"%li",[[[_dic objectForKey:@"merchant"] objectForKey:@"automatic"] integerValue]];
    if ([automid integerValue] == 2) {
        shop.labKQ.text = @"开启一键自动接单";
    }else{
        shop.labKQ.text = @"关闭一键自动接单";
    }
    NSInteger tag = [automid integerValue];
    shop.labKQ.tag = tag;
    [shop.labKQ addGestureRecognizer:tap1];
    
    shop.labName.text = [[_dic objectForKey:@"merchant"] objectForKey:@"name"];
    //star
    StarView *starView = [[StarView alloc] init];
    NSString *star = [[_dic objectForKey:@"merchant"] objectForKey:@"star"];
    NSInteger index = [star integerValue];
    starView.showStar = index * 20;
    [shop.StarView addSubview:starView];
    [starView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(shop.StarView.centerX);
        make.top.equalTo(shop.StarView.top);
        make.width.equalTo(80);
        make.height.equalTo(16);
    }];
    
    shop.labTodayJD.text = [_dic objectForKey:@"counttodayincome"];
    shop.labTodaySR.text = [_dic objectForKey:@"todayincome"];
    NSString *ison = [NSString stringWithFormat:@"%li",[[[_dic objectForKey:@"merchant"] objectForKey:@"ison"] integerValue]];
    if ([ison integerValue] == 2) {
        shop.labState.text = @"营业中";
    }else{
        shop.labState.text = @"打烊中";
    }
    
}
//打样和上班
- (void)clickWork{
    NSString *ison = [NSString stringWithFormat:@"%li",[[[_dic objectForKey:@"merchant"] objectForKey:@"ison"] integerValue]];//2上班 3打烊
    NSString *str;
    if ([ison integerValue] == 2) {
        str = @"3";
    }else{
        str = @"2";
    }
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    NSString *dpid = [NSString stringWithFormat:@"%li",[[[_dic objectForKey:@"merchant"] objectForKey:@"id"] integerValue]];
    [parmas setObject:dpid forKey:@"id"];
    [parmas setObject:str forKey:@"ison"];
    NSLog(@"parmas:%@",parmas);
    NSString *url = [NSString stringWithFormat:@"%@/App/Merchantcentre/proofing",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",[responseObject objectForKey:@"mssage"]);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            //shop.labState.text = @"";
            [self loadData];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}

//开启接单
- (void)clickKQ{
    NSString *automatic = [NSString stringWithFormat:@"%li",[[[_dic objectForKey:@"merchant"] objectForKey:@"automatic"] integerValue]];//2为等待接单3为自动接单
    NSString *str;
    if ([automatic integerValue] == 2) {
        str = @"3";
    }else{
        str = @"2";
    }
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    NSString *dpid = [NSString stringWithFormat:@"%li",[[[_dic objectForKey:@"merchant"] objectForKey:@"id"] integerValue]];
    [parmas setObject:dpid forKey:@"id"];
    [parmas setObject:str forKey:@"automatic"];
    NSLog(@"parmas:%@",parmas);//2为手动接单3为自动接单
    NSString *url = [NSString stringWithFormat:@"%@/App/Merchantcentre/automaticorder",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
//        NSLog(@"res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            [self loadData];
        }else{
            [SVProgressHUD showErrorWithStatus:[responseObject objectForKey:@"mssage"]];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 3) {
        return 1;
    }
    
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ShopCenterCell *cell = [ShopCenterCell tableViewCellWithTableView:tableView];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.avater.image =  [UIImage imageNamed:_arr1Img[indexPath.row]];
        cell.titleLabel.text = _arr1Tit[indexPath.row];
        cell.subTitleLabel.text = _arr1Det[indexPath.row];
        return cell;
    }else if (indexPath.section == 1){
        ShopCenterCell *cell = [ShopCenterCell tableViewCellWithTableView:tableView];
        cell.avater.image =  [UIImage imageNamed:_arr2Img[indexPath.row]];
        cell.titleLabel.text = _arr2Tit[indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        return cell;
    }else if (indexPath.section == 2){
        ShopCenterCell *cell = [ShopCenterCell tableViewCellWithTableView:tableView];
        cell.avater.image =  [UIImage imageNamed:_arr3Img[indexPath.row]];
        cell.titleLabel.text = _arr3Tit[indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        NSString *tgcountuser = [NSString stringWithFormat:@"%li人",[[_dic objectForKey:@"tgcountuser"] integerValue]];
        NSString *horseman = [NSString stringWithFormat:@"%li人",[[_dic objectForKey:@"horseman"] integerValue]];
        if (indexPath.row == 0) {
            cell.subTitleLabel.text = tgcountuser;
        }else{
            cell.subTitleLabel.text = horseman;
        }
        
        return cell;
    }else{
        ShopCenterCell *cell = [ShopCenterCell tableViewCellWithTableView:tableView];
        cell.avater.image =  [UIImage imageNamed:@"客服"];;
        cell.titleLabel.text = @"客服中心";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        return cell;
    }
}

//头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];
        view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
        //待接单
        UIView *viewL = [[UIView alloc] initWithFrame:CGRectMake(0, 10, ScreenWidth/3, 80.0)];
        viewL.backgroundColor = [UIColor whiteColor];
        [view addSubview:viewL];
        
        UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"开关机_待接单"]];
        [viewL addSubview:image1];
        [image1 makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(viewL.centerX);
            make.top.equalTo(viewL.top).offset(14);
        }];
        
        UILabel *lab1 = [[UILabel alloc] init];
        lab1.text = @"待接单";
        lab1.font = [UIFont systemFontOfSize:15.0];
        [viewL addSubview:lab1];
        [lab1 makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(image1.centerX);
            make.top.equalTo(image1.bottom).offset(8);
        }];
        
        UILabel *line1 = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/3, 20, 1, 60)];
        line1.backgroundColor = [UIColor grayColor];
        [view addSubview:line1];
        
        UILabel *line2 = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/3*2, 20, 1, 60)];
        line2.backgroundColor = [UIColor grayColor];
        [view addSubview:line2];
        
        //已接单
        UIView *viewC = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth/3, 10, ScreenWidth/3, 80.0)];
        viewC.backgroundColor = [UIColor whiteColor];
        [view addSubview:viewC];
        
        UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"已接单"]];
        [viewC addSubview:image2];
        [image2 makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(viewC.centerX);
            make.top.equalTo(viewC.top).offset(10);
        }];
        
        UILabel *lab2 = [[UILabel alloc] init];
        lab2.text = @"已接单";
        lab2.font = [UIFont systemFontOfSize:15.0];
        [viewC addSubview:lab2];
        [lab2 makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(image2.centerX);
            make.top.equalTo(image2.bottom).offset(8);
        }];
        //已完成订单
        UIView *viewR = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth/3*2, 10, ScreenWidth/3, 80.0)];
        viewR.backgroundColor = [UIColor whiteColor];
        [view addSubview:viewR];
        
        UIImageView *image3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"订单-1"]];
        [viewR addSubview:image3];
        [image3 makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(viewR.centerX);
            make.top.equalTo(viewR.top).offset(10);
        }];
        
        UILabel *lab3 = [[UILabel alloc] init];
        lab3.text = @"已完成订单";
        lab3.font = [UIFont systemFontOfSize:15.0];
        [viewR addSubview:lab3];
        [lab3 makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(image3.centerX);
            make.top.equalTo(image3.bottom).offset(8);
        }];
        viewL.userInteractionEnabled = YES;
        viewC.userInteractionEnabled = YES;
        viewR.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click1)];
        [viewL addGestureRecognizer:tap1];
        
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click2)];
        [viewC addGestureRecognizer:tap2];
        
        UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click3)];
        [viewR addGestureRecognizer:tap3];
        return view;
    }else if (section == 1){
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
        view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
        return view;
    }else{
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
        view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
        return view;
    }
    
}
//待接单
- (void)click1{
    DJDViewController *djd = [[DJDViewController alloc] init];
    djd.businessId = _mer_id;
    djd.merType = @"1";
    [self.navigationController pushViewController:djd animated:YES];
}
//已接单
- (void)click2{
    YJDViewController *yjd = [[YJDViewController alloc] init];
    yjd.businessId = _mer_id;
    yjd.merType = @"2";
    [self.navigationController pushViewController:yjd animated:YES];
}
//已完成
- (void)click3{
    YWCViewController *djd = [[YWCViewController alloc] init];
    djd.businessId = _mer_id;
    djd.merType = @"3";
    [self.navigationController pushViewController:djd animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 100.0;
    }
    return 10;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *merid = [NSString stringWithFormat:@"%li",[[[_dic objectForKey:@"merchant"] objectForKey:@"id"] integerValue]];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            YueViewController *yue = [[YueViewController alloc] init];
            yue.price = [_dic objectForKey:@"usermoney"];
            [self.navigationController pushViewController:yue animated:YES];
        }else if (indexPath.row == 1){
            PingJiaViewController *pingjia = [[PingJiaViewController alloc] init];
            pingjia.merid = merid;
            NSInteger index = [self.merchanttype integerValue];
            NSString *subUrlString = [self.commentUrlArray objectAtIndex:index];
            pingjia.url = [NSString stringWithFormat:@"%@%@",urlHead,subUrlString];
            [self.navigationController pushViewController:pingjia animated:YES];
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            HouseMaGerViewController *manager = [[HouseMaGerViewController alloc] init];
            manager.merType = _merchanttype;
            manager.merid = merid;
            [self.navigationController pushViewController:manager animated:YES];
        }else{
            DPZLViewController *dpzl = [[DPZLViewController alloc] init];
            NSString *merid = [NSString stringWithFormat:@"%li",[[[_dic objectForKey:@"merchant"] objectForKey:@"id"] integerValue]];
            dpzl.merid = merid;
            dpzl.merchanttype = _merchanttype;
            [self.navigationController pushViewController:dpzl animated:YES];
        }
    }else if (indexPath.section == 2){
        
    }else{
        KFuViewController *kf = [[KFuViewController alloc] init];
        [self.navigationController pushViewController:kf animated:YES];
    }
    
}

//商家中心
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    NSLog(@"parmas:%@",parmas);
    NSInteger tag = [_merchanttype integerValue];
    NSString *urlfoot = urlArr[tag];
    NSString *url = [NSString stringWithFormat:@"%@%@",urlHead,urlfoot];
    NSLog(@"url:%@",url);
    [[AFHTTPSessionManager manager] GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"商家中心res:%@",responseObject);
        NSString *error = [responseObject objectForKey:@"error"];
        if ([error integerValue] == 200) {
            _dic = [responseObject objectForKey:@"result"];
            NSString *str1 = [NSString stringWithFormat:@"%@元",[_dic objectForKey:@"usermoney"]];
            NSString *str2 = [NSString stringWithFormat:@"%li",[[_dic objectForKey:@"shopcomment"] integerValue]];
            [_arr1Det replaceObjectAtIndex:0 withObject:str1];
            [_arr1Det replaceObjectAtIndex:1 withObject:str2];
            [self loadUI];
            _mer_id = [[_dic objectForKey:@"merchant"] objectForKey:@"id"];
            [_tableView reloadData];
        }
        
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
